package com.dualPointer;

public class BackspaceStringCompare0202_844 {
    public boolean backspaceCompare(String s, String t) {
        int ss = s.length()-1, tt = t.length()-1;
        int sNum = 0, tNum = 0;

        while (true){
            while (ss>=0){
                if(s.charAt(ss) == '#') sNum++;
                else {
                    if(sNum >0) sNum--;
                    else break;;
                }
                ss--;
            }
            while (tt >= 0) {
                if(t.charAt(tt) == '#') tNum++;
                else {
                    if(tNum>0) tNum--;
                    else break;
                }
                tt--;
            }

            if(ss < 0 || tt<0) break;
            if(s.charAt(ss) != t.charAt(tt)) return false;
            ss--;tt--;
        }
        return ss == -1 && tt == -1;
    }
}
